package com.paomi.client.activity.person;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.person.View.MyOrderNumView;
import com.paomi.client.activity.person.model.MyOrderNumBean;
import com.paomi.client.activity.person.presenter.MyOrderPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.fragment.order.AllOrderFragment;
import com.paomi.client.fragment.order.CollectOrderFragment;
import com.paomi.client.fragment.order.CompleteOrderFragment;
import com.paomi.client.fragment.order.DeliveryOrderFragment;
import com.paomi.client.fragment.order.ObligationOrderFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单列表页面
 */
public class MyOrderActivity extends BaseMvpActivity<MyOrderNumView, MyOrderPre> implements MyOrderNumView {
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    //全部
    @BindView(R.id.rl_all)
    RelativeLayout rl_all;
    //待付款
    @BindView(R.id.rl_payment)
    RelativeLayout rl_payment;
    //待发货
    @BindView(R.id.rl_delivery)
    RelativeLayout rl_delivery;
    //待收货
    @BindView(R.id.rl_received)
    RelativeLayout rl_received;
    //已完成
    @BindView(R.id.rl_completed)
    RelativeLayout rl_completed;
    @BindView(R.id.tv_r_0)
    TextView tvR0;
    @BindView(R.id.tv_r_1)
    TextView tvR1;
    @BindView(R.id.tv_r_2)
    TextView tvR2;
    @BindView(R.id.tv_r_3)
    TextView tvR3;
    @BindView(R.id.tv_r_4)
    TextView tvR4;

    private AllOrderFragment allOrderFragment;//全部
    private CollectOrderFragment collectOrderFragment;//待付款
    private CompleteOrderFragment completeOrderFragment;//已完成
    private DeliveryOrderFragment deliveryOrderFragment;//待发货
    private ObligationOrderFragment obligationOrderFragment;//待收货
    private List<Fragment> list;
    int pos = 0;
    private List<MyOrderNumBean.MyOrderNum> numList = new ArrayList<>();//订单数量的列表


    @Override
    protected String setTitle() {
        return getResources().getString(R.string.order_title);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void initEvent() {
        pos = getIntent().getIntExtra("pos", 0);

        allOrderFragment = new AllOrderFragment();
        collectOrderFragment = new CollectOrderFragment();
        completeOrderFragment = new CompleteOrderFragment();
        deliveryOrderFragment = new DeliveryOrderFragment();
        obligationOrderFragment = new ObligationOrderFragment();
        list = new ArrayList<Fragment>();
        list.add(allOrderFragment);
        list.add(collectOrderFragment);
        list.add(deliveryOrderFragment);
        list.add(obligationOrderFragment);
        list.add(completeOrderFragment);
        viewpager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), list));
        viewpager.setCurrentItem(pos);
        change(pos);
        // ViewPager页面切换监听
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rl_all.isClickable();
                        change(0);
                        break;
                    case 1:
                        rl_payment.isClickable();
                        change(1);
                        break;
                    case 2:
                        rl_delivery.isClickable();
                        change(2);
                        break;
                    case 3:
                        rl_received.isClickable();
                        change(3);
                        break;
                    case 4:
                        rl_completed.isClickable();
                        change(4);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rl_all.setOnClickListener(mainListener);
        rl_payment.setOnClickListener(mainListener);
        rl_delivery.setOnClickListener(mainListener);
        rl_received.setOnClickListener(mainListener);
        rl_completed.setOnClickListener(mainListener);
        getPresenter().getNumOrder("0,1,2,3,4,5,6,7");//订单状态 0 未支付 1 已支付 2 待发货 3 待收货 4 待评价（已收货） 5 已完成（已评价） 6 退款申请中 7 已退款
    }

    final View.OnClickListener mainListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rl_all:
                    viewpager.setCurrentItem(0);
                    change(0);
                    break;
                case R.id.rl_payment:
                    viewpager.setCurrentItem(1);
                    change(1);
                    break;
                case R.id.rl_delivery:
                    viewpager.setCurrentItem(2);
                    change(2);
                    break;
                case R.id.rl_received:
                    viewpager.setCurrentItem(3);
                    change(3);
                    break;
                case R.id.rl_completed:
                    viewpager.setCurrentItem(4);
                    change(4);
                    break;
            }
        }
    };

    void change(int index) {
        for (int i = 0; i < 5; i++) {
//            TextView red_text = findViewById(getResources().getIdentifier("tv_r_" + (i), "id", getPackageName()));
            TextView textView = findViewById(getResources().getIdentifier("tv_" + (i), "id", getPackageName()));
            View view = findViewById(getResources().getIdentifier("view_" + (i), "id", getPackageName()));

            if (i == index) {
//                red_text.setVisibility(View.VISIBLE);
                textView.setTextColor(getResources().getColor(R.color.main_color));
                view.setBackgroundColor(MyOrderActivity.this.getResources().getColor(R.color.main_color));
            } else {
//                red_text.setVisibility(View.GONE);
                view.setBackgroundColor(MyOrderActivity.this.getResources().getColor(R.color.white));
                textView.setTextColor(getResources().getColor(R.color.text_black_33));
            }
        }
    }

    @NonNull
    @Override
    public MyOrderPre createPresenter() {
        return new MyOrderPre();
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    /**
     * 获取总的订单数量
     *
     * @param numBean
     */
    @Override
    public void setNumData(MyOrderNumBean numBean) {
        if (numBean != null && numBean.getReturnData().size() != 0) {
            int total = 0;//订单全部数量
            numList.clear();
            numList.addAll(numBean.getReturnData());
            for (MyOrderNumBean.MyOrderNum myOrderNum : numList) {
                total += myOrderNum.getNumber();
                if (myOrderNum.getState() == 0) {
                    tvR1.setVisibility(View.VISIBLE);
                    tvR1.setText(myOrderNum.getNumber() + "");
                } else if (myOrderNum.getState() == 1) {//已支付
                    tvR2.setVisibility(View.VISIBLE);
                    tvR2.setText(myOrderNum.getNumber() + "");
                } else if (myOrderNum.getState() == 3) {
                    tvR3.setVisibility(View.VISIBLE);
                    tvR3.setText(myOrderNum.getNumber() + "");
                } else if (myOrderNum.getState() == 5) {
                    tvR4.setVisibility(View.VISIBLE);
                    tvR4.setText(myOrderNum.getNumber() + "");
                }
            }
            if (total > 0) {
                tvR0.setVisibility(View.VISIBLE);
                tvR0.setText("" + total);
            }
        }

    }
}
